package com.csun.dsms.controller;

import com.csun.dsms.comment.core.annotation.Authorization;
import com.csun.dsms.comment.core.annotation.CurrentUser;
import com.csun.dsms.comment.core.annotation.LogAnnotation;
import com.csun.dsms.comment.core.annotation.ValidateAnnotation;
import com.csun.dsms.comment.core.support.CsunMessageSource;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.model.domain.DsmsUser;
import com.csun.dsms.model.dto.user.*;
import com.csun.dsms.model.vo.menu.MenuVo;
import com.csun.dsms.model.vo.user.UserVo;
import com.csun.dsms.service.DsmsRoleMenuService;
import com.csun.dsms.service.DsmsUserService;
import com.csun.dsms.validator.DsmsUserAddValidator;
import com.csun.dsms.validator.DsmsUserEditValidator;
import com.github.pagehelper.PageInfo;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author chisj chisj@foxmail.com
 * @version 1.0.0
 * @date 2021-09-01 11:33
 * @description: 用户管理拦截器
 */
@RestController
@RequestMapping("/user")
public class DsmsUserController {

    @Resource
    private CsunMessageSource csunMessageSource;
    @Resource
    private DsmsUserService dsmsUserService;
    @Resource
    private DsmsRoleMenuService dsmsRoleMenuService;

    /**
     * 获取用户信息
     *
     * @param login the login user
     *
     * @return  the wrapper.
     */
    @Authorization
    @RequestMapping(method = RequestMethod.GET)
    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    public Wrapper user(@CurrentUser DsmsUser login) {

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "个人信息查询成功", login);
    }

    /**
     * 获取菜单树
     */
    @Authorization
    @PostMapping(value = "/queryOwnMenuTree")
    public Wrapper queryOwnMenuTree(@CurrentUser DsmsUser login) {

        List<MenuVo> tree = dsmsRoleMenuService.queryOwnMenuTree(login);

        return WrapMapper.ok(tree);
    }

    /**
     * 查询用户列表.
     *
     * @param userQueryDto the user query dto
     *
     * @return the wrapper
     */
    @Authorization
    @PostMapping(value = "/queryListWithPage")
    public Wrapper<PageInfo> queryUserListWithPage(@CurrentUser DsmsUser login,
                                                   @RequestBody UserQueryDto userQueryDto) {

        List<UserVo> userVoList = dsmsUserService.queryListWithPage(userQueryDto);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, csunMessageSource
                .getMessage("operation.success", null), new PageInfo(userVoList));
    }

    /**
     * 新增用户
     *
     * @param userDto the user dto
     *
     * @return the wrapper
     */
    @Authorization
    @ValidateAnnotation
    @PostMapping(value = "/save")
    public Wrapper addUser(@CurrentUser DsmsUser login,
                           @Validated(DsmsUserAddValidator.class) @RequestBody UserDto userDto,
                           BindingResult br) {

        Integer result = dsmsUserService.save(userDto, login);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, csunMessageSource
                    .getMessage("operation.failed", null), result);
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, csunMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 修改用户信息
     *
     * @param login     the login user
     * @param userDto   the user dto
     *
     * @return  the wrapper.
     */
    @Authorization
    @ValidateAnnotation
    @PostMapping(value = "/edit")
    public Wrapper editUser(@CurrentUser DsmsUser login,
                            @Validated(DsmsUserEditValidator.class) @RequestBody UserDto userDto) {

        Integer result = dsmsUserService.edit(userDto, login);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, csunMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, csunMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 根据Id修改用户状态.
     *
     * @param modifyUserStatusDto the modify user status dto
     *
     * @return the wrapper
     */
    @Authorization
    @ValidateAnnotation
    @PostMapping(value = "/modifyUserStatusById")
    public Wrapper<Integer> modifyUserStatusById(@CurrentUser DsmsUser login,
                                                 @Valid @RequestBody ModifyUserStatusDto modifyUserStatusDto) {

        DsmsUser dsmsUser = new DsmsUser();
        dsmsUser.setId(modifyUserStatusDto.getUserId());
        dsmsUser.setStatus(modifyUserStatusDto.getStatus());

        int result = dsmsUserService.modifyUserStatusById(dsmsUser, login);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, csunMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, csunMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 通过Id删除用户.
     *
     * @param userId the user id
     *
     * @return the wrapper
     */
    @Authorization
    @PostMapping(value = "/deleteById/{userId}")
    public Wrapper<Integer> deleteUserById(@PathVariable Long userId) {

        Integer result = dsmsUserService.deleteUserById(userId);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, csunMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, csunMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 用户修改密码
     *
     * @param userModifyPwdDto the user modify pwd dto
     *
     * @return the wrapper
     */
    @Authorization
    @ValidateAnnotation
    @PostMapping(value = "/modifyUserPwd")
    public Wrapper<Integer> modifyUserPwd(@CurrentUser DsmsUser login,
                                          @Valid @RequestBody UserModifyPwdDto userModifyPwdDto,
                                          BindingResult br) {

        Integer result = dsmsUserService.userModifyPwd(userModifyPwdDto, login);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, csunMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, csunMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 重置用户密码
     *
     * @param login         the login user
     * @param resetPwdDto   the reset password dto
     * @param br
     *
     * @return  wrapper
     */
    @Authorization
    @ValidateAnnotation
    @PostMapping(value = "/resetPwd")
    public Wrapper resetPwd(@CurrentUser DsmsUser login,
                            @Validated @RequestBody ResetPwdDto resetPwdDto,
                            BindingResult br) {

        Integer result = dsmsUserService.resetPwd(resetPwdDto, login);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, csunMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, csunMessageSource
                .getMessage("operation.success", null), result);
    }

}
